package com.gy.bearhelps.help;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.BitmapDescriptor;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;
import com.gy.bearhelps.R;
import com.gy.bearhelps.base.BaseActivity;
import com.gy.bearhelps.home.bean.MapSearchBean;
import com.gy.bearhelps.navi.SingleRouteCalculateActivity;
import com.gy.bearhelps.util.map.MapBaseDrawable;
import com.gy.bearhelps.util.map.MapDetailMarker;

/**
 * Created by 高岳 on 2016/11/8.
 * Describe:黄页地图标记
 */
public class HelpMapActivity extends BaseActivity implements AMap.InfoWindowAdapter, AMap.OnInfoWindowClickListener,
        AMap.OnMarkerClickListener {

    private ImageView imgBack;
    private MapView mapView;
    private TextView tvTitle;
    private AMap aMap;
    private LatLng center;//地图中心点
    private double endLat, endLong;
    private String title, address;
    private MapBaseDrawable mMapBaseDrawable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_search);
        imgBack = (ImageView) findViewById(R.id._back);
        tvTitle = (TextView) findViewById(R.id._title);
        mapView = (MapView) findViewById(R.id.activity_map);
        mapView.onCreate(savedInstanceState);
        aMap = mapView.getMap();
        title = getIntent().getStringExtra("title");
        address = getIntent().getStringExtra("address");
        endLat = getIntent().getDoubleExtra("endLat", 0);
        endLong = getIntent().getDoubleExtra("endLong", 0);
        center = new LatLng(endLat, endLong);
        tvTitle.setText(title);
        setListeners();
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    public void setListeners() {
        MapSearchBean searchBean = new MapSearchBean();
        searchBean.address = address;
        searchBean.title = title;
        searchBean.x = endLong + "";
        searchBean.y = endLat + "";
        mMapBaseDrawable = new MapDetailMarker(this);
        Bitmap bitmap = mMapBaseDrawable.createMarker(searchBean);
        BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(bitmap);
        imgBack.setOnClickListener(this);
        aMap.getUiSettings().setMyLocationButtonEnabled(false);// 设置默认定位按钮是否显示
        aMap.getUiSettings().setZoomControlsEnabled(false);//设置地图缩放按钮是否显示
        aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位，false表示隐藏定位层并不可触发定位，默认是false
        aMap.moveCamera(CameraUpdateFactory.zoomTo(16));//设置地图zoom级别
        aMap.moveCamera(CameraUpdateFactory.changeLatLng(center));
        aMap.setInfoWindowAdapter(this);// 设置自定义InfoWindow样式
        aMap.setOnMarkerClickListener(this);
        aMap.setOnInfoWindowClickListener(this);
        aMap.addMarker(new MarkerOptions().position(center).icon(icon).draggable(true));
    }

    @Override
    public View getInfoWindow(Marker marker) {
        View windowView = View.inflate(this, R.layout.view_amap_detail_marker, null);
        TextView tvTitle = (TextView) windowView.findViewById(R.id.view_amap_detail_tv_title);
        TextView tvAddress = (TextView) windowView.findViewById(R.id.view_amap_detail_tv_address);
        tvTitle.setText(marker.getTitle());
        tvAddress.setText(marker.getSnippet());
        return windowView;
    }

    @Override
    public View getInfoContents(Marker marker) {
        return null;
    }

    @Override
    public void onInfoWindowClick(Marker marker) {
    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        Intent intent = new Intent(this, SingleRouteCalculateActivity.class);
        intent.putExtra("endLat", marker.getPosition().latitude);
        intent.putExtra("endLong", marker.getPosition().longitude);
        startActivity(intent);
        return false;
    }
}
